package com.example.pceb.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pceb.entity.OperationLog;
import com.example.pceb.mapper.OperationLogMapper;
import com.example.pceb.service.OperationLogService;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {
    @Override
    public IPage<OperationLog> pageQuery(Page<OperationLog> page, String username, String operationType) {
        QueryWrapper<OperationLog> wrapper = new QueryWrapper<>();
        if (username != null && !username.isEmpty()) {
            wrapper.like("username", username);
        }
        if (operationType != null && !operationType.isEmpty()) {
            wrapper.eq("operation_type", operationType);
        }
        wrapper.orderByDesc("create_time");
        return this.page(page, wrapper);
    }

    @Override
    public Map<String, Object> getStats() {
        Map<String, Object> stats = new HashMap<>();
        QueryWrapper<OperationLog> wrapper = new QueryWrapper<>();
        stats.put("total", this.count(wrapper));
        stats.put("login", this.count(new QueryWrapper<OperationLog>().eq("operation_type", "登录")));
        stats.put("modify", this.count(new QueryWrapper<OperationLog>().eq("operation_type", "信息修改")));
        stats.put("apply", this.count(new QueryWrapper<OperationLog>().eq("operation_type", "项目申请")));
        return stats;
    }

    @Override
    public void clearAll() {
        this.remove(new QueryWrapper<>());
    }
} 